package com.bugman.managed;

import com.bugman.entity.User;
import com.bugman.exceptions.UsernameExistException;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Ediczech
 */
public class RegisterUserBean extends BaseBean {

    private String password;
    private User user = new User();
    private ResourceBundle rb = getBundle();

    public String register() {
        try {
            userService.registerUser(user);
        } catch (UsernameExistException ex) {
            logger.error("Uzivatel se jmenem: " + user.getNick() + "jiz existuje.");
            return "fail";
        }
        logger.info("Registrace uzivatele " + user + " probehla v poradku.");
        return "success";
    }

    public void usernameValidator(FacesContext context, UIComponent component,
            Object object) throws ValidatorException {

        String username = (String) object;
        if (userService.usernameExist(username)) {
            throw new ValidatorException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    rb.getString("registerUser_error_usernameExist"),
                    rb.getString("registerUser_error_usernameExist_detail")));
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
